package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.UserPassengerConverter;
import com.dycx.user.dto.UserPassengerDTO;
import com.dycx.user.entity.UserPassengerDO;
import com.dycx.user.service.UserPassengerService;
import com.dycx.user.vo.UserPassengerVO;
import com.dycx.user.mapper.UserPassengerMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 乘车人(UserPassenger)表服务实现类
 *
 * @author wuzongbing
 * @since 2025-01-01 21:11:39
 */

@Service
@AllArgsConstructor
public class UserPassengerImpl extends ServiceImpl<UserPassengerMapper, UserPassengerDO> implements UserPassengerService {

    private UserPassengerMapper userPassengerMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<UserPassengerVO> getPage(UserPassengerDTO dto) {
        LambdaQueryWrapper<UserPassengerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getId()), UserPassengerDO::getId, dto.getId());
        wrapper.orderByDesc(UserPassengerDO::getId);

        Page<UserPassengerDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<UserPassengerVO> voPage = UserPassengerConverter.entityToVoPage(entityPage);
        return voPage;
    }

    /**
     * 查询列表
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public List<UserPassengerVO> getList(UserPassengerDTO dto) {
        LambdaQueryWrapper<UserPassengerDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getStatus()), UserPassengerDO::getStatus, dto.getStatus());
        wrapper.orderByDesc(UserPassengerDO::getId);

        List<UserPassengerDO> entityList = list(wrapper);

        return UserPassengerConverter.entityToVoList(entityList);
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public UserPassengerVO load(Integer id) {
        UserPassengerDO entity = getById(id);

        return UserPassengerConverter.entityToVo(entity);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(UserPassengerDTO dto) {
        UserPassengerDO entity = BeanUtil.toBean(dto, UserPassengerDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(UserPassengerDTO dto) {
        UserPassengerDO entity = BeanUtil.toBean(dto, UserPassengerDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return userPassengerMapper.getMaxSort();
    }
}

